package com.boyicloud.exchangeconsole.commnon

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.TypeReference
import com.boyicloud.exchangeconsole.domain.ClientInfo
import com.boyicloud.exchangeconsole.domain.User
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
import org.springframework.stereotype.Component
import org.springframework.web.client.RestTemplate

@Component
class HttpClient {


    @Autowired
    lateinit var restTemplate: RestTemplate

    var token = ""

    fun equipmentList(): List<ClientInfo> {
        val json =
            restTemplate.exchange(
                "${Constant.HTTP_BASE_URL}client/equipment/list",
                HttpMethod.GET,
                onlyToken(),
                String::class.java,
            )
        val result = JSON.parseObject(json.body, object : TypeReference<Result<List<ClientInfo>>>() {})
        return result.data
    }

    fun softwareList(): List<ClientInfo> {
        val json =
            restTemplate.exchange(
                "${Constant.HTTP_BASE_URL}client/software/list",
                HttpMethod.GET,
                onlyToken(),
                String::class.java,
            )
        val result = JSON.parseObject(json.body, object : TypeReference<Result<List<ClientInfo>>>() {})
        return result.data
    }

    fun list(): ListResp {
        val json = restTemplate.exchange(
            "${Constant.HTTP_BASE_URL}client/list",
            HttpMethod.GET,
            onlyToken(),
            String::class.java,
        )
        val result = JSON.parseObject(json.body, object : TypeReference<Result<ListResp>>() {})
        return result.data
    }

    fun exchange(equipmentId: Long, softwareId: Long): String {
        val json = restTemplate.postForObject(
            "${Constant.HTTP_BASE_URL}client/exchange",
            jsonWithToken(mutableMapOf("equipmentId" to equipmentId, "softwareId" to softwareId)),
            String::class.java,
        )
        val result = JSON.parseObject(json, object : TypeReference<Result<String>>() {})
        return if (result.success) result.data else result.errMessage
    }

    fun offline(ids: Array<Long>): String {
        val json =
            restTemplate.postForObject(
                "${Constant.HTTP_BASE_URL}client/offline",
                jsonWithToken(mutableMapOf("ids" to ids)),
                String::class.java,
            )
        val result = JSON.parseObject(json, object : TypeReference<Result<String>>() {})
        return if (result.success) result.data else result.errMessage
    }

    fun disconnect(id: Long): String {
        val json = restTemplate.exchange(
            "${Constant.HTTP_BASE_URL}client/disconnect?id=$id",
            HttpMethod.GET,
            onlyToken(),
            String::class.java,
        )
        val result = JSON.parseObject(json.body, object : TypeReference<Result<String>>() {})
        return if (result.success) result.data else result.errMessage
    }

    fun reset(): String {
        val json = restTemplate.getForObject("${Constant.HTTP_BASE_URL}client/reset", String::class.java)
        val result = JSON.parseObject(json, object : TypeReference<Result<String>>() {})
        return if (result.success) result.data else result.errMessage
    }

    fun login(user: MutableMap<String, String>): Boolean {
        val json =
            restTemplate.postForObject("${Constant.HTTP_BASE_URL}user/login", user, String::class.java)
        val result = JSON.parseObject(json, object : TypeReference<Result<User>>() {})
        if (result.success) {
            token = result.data.token as String
        }
        return result.success
    }


    fun jsonWithToken(data: Any): HttpEntity<Any> {
        return HttpEntity(data, HttpHeaders().apply {
            this.set(HttpHeaders.CONTENT_TYPE, "application/json;charset=utf-8")
            this["token"] = token
        })
    }

    fun onlyToken(): HttpEntity<Any> {
        return HttpEntity(
            HttpHeaders().apply {
                this["token"] = token
            }
        )
    }
}
